module.exports = function ($scope, $modalInstance, $resource, $modal, item, toaster) {

    $scope.office = item;
    $scope.temp_group = {
        isReadly: false,
        code: "",
        name: ""
    };
    $scope.temp_update_group = {
        isReadly: false,
        code: "",
        name: ""
    };
    $scope.groupList = [];
    $scope.groupMap = {};
    $scope.openMap = {};
    $scope.open_toggle = function (index) {
        $scope.groupList[index].is_open = !$scope.groupList[index].is_open;
        $scope.openMap[$scope.groupList[index].code] = $scope.groupList[index].is_open;
    }

    $scope.update = function (index) {
        $scope.groupList[index].is_edit = true;
        $scope.temp_update_group = angular.copy($scope.groupList[index]);
        $scope.temp_update_group.isReadly = false;
    }
    $scope.giveUpdate = function (index) {
        $scope.groupList[index].is_edit = false;
        $scope.temp_update_group = {
            isReadly: false,
            code: "",
            name: ""
        };
    }
    $scope.save = function (index) {
        $resource('/api/as/sc/office/updateDownCompanyGroup', {}, {}).save($scope.temp_update_group, function (res) {
            if (res.errcode === 0) {
                $scope.groupList[index].name = $scope.temp_update_group.name;
                $scope.temp_update_group = {
                    isReadly: false,
                    code: "",
                    name: ""
                };
                $scope.groupList[index].is_edit = false;
                $scope.temp_update_group.isReadly = false;
                toaster.success({ title: '', body: '修改成功' });
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }

    $scope.readly = function () {
        for (let index = 0; index < $scope.groupList.length; index++) {
            const element = $scope.groupList[index];
            element.is_open = false;
        }
        $scope.temp_group.isReadly = true;
    }
    $scope.give = function () {
        $scope.temp_group = {
            isReadly: false,
            code: "",
            name: ""
        };
    }
    $scope.create = function () {
        $resource('/api/as/sc/office/createDownCompanyGroup', {}, {}).save($scope.temp_group, function (res) {
            if (res.errcode === 0) {
                $scope.temp_group = {
                    isReadly: false,
                    code: "",
                    name: ""
                };
                $scope.loadGroupList();
                toaster.success({ title: '', body: '创建成功' });
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }
    $scope.updatePrice = function (obj, index, parent_index) {
        if (obj.floating_price === null || obj.floating_price === undefined || obj.floating_price < 0) {
            toaster.warning({title: '', body: '请输入正确的金额'});
            return false;
        }
        let para = {
            company_code: obj.code,
            floating_price: obj.floating_price,
            sale_code: item.code
        }
        $resource('/api/as/tc/saleauth/updateFloatingPrice', {}, {}).save(para, function (res) {
            if (res.errcode === 0) {
                $scope.groupList[parent_index].childList[index].is_edit = false;
                toaster.success({ title: '', body: '修改成功' });
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }

    $scope.loadGroupList = function () {
        $resource('/api/as/sc/office/findDownCompanyGroupList', {}, {}).save($scope.temp_group, function (res) {
            if (res.errcode === 0) {
                $scope.groupList = res.data;
                $scope.group_select_list = res.data;
                $scope.groupList.push({
                    floating_price: 0,
                    code: 'weifenzu',
                    name: '未分组',
                    childList: []
                })
                for (let index = 0; index < $scope.groupList.length; index++) {
                    const element = $scope.groupList[index];
                    element.is_auth = false;
                    element.is_open = $scope.openMap[element.code] || (index == $scope.groupList.length - 1? true: false);
                    element.floating_price = 0;
                    $scope.groupMap[element.code] = index;
                    $scope.openMap[element.code] = $scope.openMap[element.code] || false;
                }
                $scope.load();
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }

    $scope.sel_list = [];
    $scope.search = {};

    $scope.relaMap = {};
    $scope.load = function () {

        $resource('/api/ac/tc/ticketSaleAuthService/findDownstreams', {}, {}).save({ 'sale_code': item.code }, function (resultOne) {
            if (resultOne.errcode === 0) {
                for (let index = 0; index < resultOne.data.length; index++) {
                    const element = resultOne.data[index];
                    element.describe = '新建分销商供应商分组关系';
                    element.is_purchase = (element.select_type == '1');
                    element.floating_price = element.floating_price || 0;
                    element.group_code = element.group_code || $scope.group_select_list[$scope.group_select_list.length - 1].code;
                    if (element.is_purchase) {
                        $scope.groupList[$scope.groupMap[element.group_code]].is_auth = true;
                    }
                    $scope.groupList[$scope.groupMap[element.group_code]].floating_price = element.floating_price;
                    if (!$scope.groupList[$scope.groupMap[element.group_code]].childList) {
                        $scope.groupList[$scope.groupMap[element.group_code]].childList = [];
                    }
                    $scope.groupList[$scope.groupMap[element.group_code]].childList.push(element);
                }
            } else {
                toaster.error({ title: '', body: resultOne.errmsg });
            }
        });

        // $resource('/api/as/sc/office/findRelationList', {}, {}).save($scope.temp_group, function (res) {
        //     if (res.errcode === 0) {
        //         $scope.relation_list = res.data;
        //         for (let index = 0; index < $scope.relation_list.length; index++) {
        //             const element = $scope.relation_list[index];
        //             $scope.relaMap[element.down_company_code] = element.group_code;
        //         }
        //         $resource('/api/as/tc/saleauth/findDownstreamList', {}, {}).save({ 'sale_code': item.code }, function (resultTwo) {
        //             if (resultTwo.errcode === 0) {
        //                 $scope.company_list = resultTwo.data;
        //                 for (let index = 0; index < $scope.company_list.length; index++) {
        //                     const element = $scope.company_list[index];
        //                     element.code = element.downstream_company_code;
        //                     element.name = element.target_company_name;
        //                     element.is_purchase = true;
        //                     element.group_code = $scope.group_select_list[$scope.group_select_list.length - 1].code;
        //                     if ($scope.relaMap[element.code] && $scope.groupMap[$scope.relaMap[element.code]] != undefined) {
        //                         if (!$scope.groupList[$scope.groupMap[$scope.relaMap[element.code]]].childList) {
        //                             $scope.groupList[$scope.groupMap[$scope.relaMap[element.code]]].childList = [];
        //                         }
        //                         element.group_code = $scope.relaMap[element.code];
        //                         $scope.groupList[$scope.groupMap[$scope.relaMap[element.code]]].childList.push(element);
        //                         $scope.groupList[$scope.groupMap[$scope.relaMap[element.code]]].is_auth = true;
        //                     } else {
        //                         $scope.groupList[$scope.groupList.length - 1].childList.push(element);
        //                         $scope.groupList[$scope.groupList.length - 1].is_auth = true;
        //                     }
        //                 }
        //                 $resource('/api/ac/tc/ticketSaleAuthService/findDownCompanyCode', {}, {}).save({ 'sale_code': item.code }, function (resultOne) {
        //                     if (resultOne.errcode === 0) {
        //                         $scope.office_list = [];
        //                         for (let index = 0; index < resultOne.data.length; index++) {
        //                             const element = resultOne.data[index];
        //                             element.floating_price = 0;
        //                             element.group_code = $scope.group_select_list[$scope.group_select_list.length - 1].code;
        //                             if ($scope.relaMap[element.code] && $scope.groupMap[$scope.relaMap[element.code]] != undefined) {
        //                                 if (!$scope.groupList[$scope.groupMap[$scope.relaMap[element.code]]].childList) {
        //                                     $scope.groupList[$scope.groupMap[$scope.relaMap[element.code]]].childList = [];
        //                                 }
        //                                 $scope.groupList[$scope.groupMap[$scope.relaMap[element.code]]].childList.push(element);
        //                                 element.group_code = $scope.relaMap[element.code];
        //                             } else {
        //                                 $scope.groupList[$scope.groupList.length - 1].childList.push(element);
        //                             }
        //                         }
        //                     } else {
        //                         toaster.error({ title: '', body: resultOne.errmsg });
        //                     }
        //                 });
        //             } else {
        //                 toaster.error({ title: '', body: resultTwo.errmsg });
        //             }
        //         });
        //     } else {
        //         toaster.error({ title: '', body: resultTwo.errmsg });
        //     }
        // });
    }
    $scope.loadGroupList();

    $scope.change1 = function (obj, flag, parent_index) {
        $scope.groupList[parent_index].is_open = true;
        if(!confirm(flag? "确认全部开启": "确认全部关闭")){
            return false;
        }
        if (flag) {
            let company_list = [];
            for (let index = 0; index < $scope.groupList[parent_index].childList.length; index++) {
                const element = $scope.groupList[parent_index].childList[index];
                let para = {
                    company_code: element.code,
                    company_name: element.name,
                    floating_price: element.floating_price,
                    sale_code: $scope.office.code
                }
                if (element.is_purchase) {
                    continue;
                }
                company_list.push(para);
            }
            $resource('/api/ac/tc/ticketSaleAuthService/save', {}, {}).save({ 'company_list': company_list }, function (res) {
                if (res.errcode === 0) {
                    for (let index = 0; index < $scope.groupList[parent_index].childList.length; index++) {
                        const element = $scope.groupList[parent_index].childList[index];
                        if (element.is_purchase) {
                            continue;
                        }
                        element.is_purchase = true;
                    }
                    toaster.success({ title: '', body: '授权成功' });
                } else {
                    element.is_purchase = !flag;
                    toaster.error({ title: '', body: res.errmsg });
                }
            });
        } else {
            for (let index = 0; index < $scope.groupList[parent_index].childList.length; index++) {
                const element = $scope.groupList[parent_index].childList[index];
                let para = {
                    company_code: element.code,
                    company_name: element.name,
                    floating_price: element.floating_price,
                    sale_code: $scope.office.code
                }
                if (!element.is_purchase) {
                    if (index == ($scope.groupList[parent_index].childList.length - 1)) {
                        toaster.success({ title: '', body: '取消授权成功' });
                    }
                    continue;
                }
                $resource('api/as/tc/saleauth/delDown', {}, {}).save(para, function (res) {
                    if (res.errcode === 0) {
                        element.is_purchase = false;
                        if (index == ($scope.groupList[parent_index].childList.length - 1)) {
                            toaster.success({ title: '', body: '取消授权成功' });
                        }
                    } else {
                        element.is_purchase = !flag;
                        toaster.error({ title: '', body: res.errmsg });
                    }
                });
            }
        }
    }

    $scope.change2 = function (obj, flag, index, parent_index) {
        const element = $scope.groupList[parent_index].childList[index];
        let para = {
            company_code: element.code,
            company_name: element.name,
            floating_price: element.floating_price,
            sale_code: $scope.office.code
        }
        if (flag) {
            $resource('/api/ac/tc/ticketSaleAuthService/save', {}, {}).save({ 'company_list': [para] }, function (res) {
                if (res.errcode === 0) {
                    element.is_purchase = flag;
                    toaster.success({ title: '', body: '授权成功' });
                } else {
                    element.is_purchase = !flag;
                    toaster.error({ title: '', body: res.errmsg });
                }
            });
        } else {
            $resource('api/as/tc/saleauth/delDown', {}, {}).save(para, function (res) {
                if (res.errcode === 0) {
                    element.is_purchase = flag;
                    toaster.success({ title: '', body: '取消授权成功' });
                } else {
                    element.is_purchase = !flag;
                    toaster.error({ title: '', body: res.errmsg });
                }
            });
        }
    }

    $scope.saveGroup = function (para, index, parent_index) {
        $resource('/api/as/sc/office/createRelation', {}, {}).save(para, function (res) {
            if (res.errcode === 0) {
                if (!$scope.groupList[$scope.groupMap[para.group_code]].childList) {
                    $scope.groupList[$scope.groupMap[para.group_code]].childList = []
                }
                // if (para.is_purchase !== $scope.groupList[$scope.groupMap[para.group_code]].is_auth) {
                //     para.is_purchase = $scope.groupList[$scope.groupMap[para.group_code]].is_auth
                //     $scope.change2(para, para.is_purchase, index, parent_index);
                // }
                $scope.groupList[$scope.groupMap[para.group_code]].childList.push(angular.copy(para));
                $scope.groupList[parent_index].childList.splice(index, 1);
                // toaster.success({ title: '', body: '修改成功' });
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }

    $scope.change = function (index, parent_index) {
        $scope.groupList[parent_index].childList[index].is_edit = true;
    }

    //确认
    $scope.ok = function () {
        $modalInstance.close($scope.sel_list.length);
    }

    //取消
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };

};